import SnowflakeId from "snowflake-id";
const { Op } = require("sequelize");
const UserModels = require('../models/user');

interface admInfo {
    id: number
    email: string,
    headImage: string,
    logname: string,
    mobilePhone: string,
    name: string,
    password: string,
    role: number,
    sex: string,
}

class AdmService {
    // 获取管理员列表
    public async getAdmList() {
        const res: any = await UserModels.findAndCountAll({
            where: {
                role: {
                    [Op.gt]: 8
                }
            },
        })
        return res ? res : null
    }
    // 分页获取管理员列表
    public async getAdmListByPageInfo(pageNum: number, pageSize: number) {
        const offset = (pageNum - 1) * pageSize
        const { count, rows } = await UserModels.findAndCountAll({
            where: {
                role: {
                    [Op.gt]: 8
                }
            },
            offset: offset,
            limit: pageSize * 1
        })
        return {
            pageNum,
            pageSize,
            total: count,
            list: rows
        }
    }
    // 根据名字模糊获取管理员列表
    public async getBylogname(logname: string) {
        const res: any = await UserModels.findAndCountAll({
            where: {
                logname: {
                    [Op.like]: `%${logname}%`
                },
                role: {
                    [Op.gt]: 8
                }
            },
        })
        return res ? res : null
    }
    // 分页根据名字模糊获取管理员列表
    public async getBylognameAndPageInfo(pageNum: number, pageSize: number, logname: string) {
        const offset = (pageNum - 1) * pageSize
        const { count, rows } = await UserModels.findAndCountAll({
            where: {
                logname: {
                    [Op.like]: `%${logname}%`
                },
                role: {
                    [Op.gt]: 8
                }
            },
            offset: offset,
            limit: pageSize * 1
        })
        return {
            pageNum,
            pageSize,
            total: count,
            list: rows
        }
    }
    // 获取授权码
    public async getCode(role: number) {
        const res = await UserModels.findOne({
            where: {
                role
            }
        })
        return res? res.dataValues.tokensecretkey : null
    }
    // 根据id获取管理员
    public async getInfoById(id: number) {
        const res = await  UserModels.findOne({
            where: {
                id,
                role: {
                    [Op.gt]: 8
                }
            }
        })
        return res? res.dataValues : null
    }
    // 添加管理员
    public async addAdmUser(params: admInfo) {
        const id = new SnowflakeId();
        params.id = id.generate();
        const res = await UserModels.create(params)
        return res ? res.dataValues : null
    }
    // 更新管理员
    public async update(id: number, params: admInfo) {
        const whereOpt = { id }
        const res = await UserModels.update(params, { where: whereOpt })
        console.log(params)
        return res[0] > 0
    }
    // 删除管理员
    public async deleteById(id: number) {
        const res: any = await UserModels.destroy({
            where: { id }
        })
        return res > 0
    }
}

module.exports = new AdmService()
// 解决同目录下多个ts文件都引入Op,会报错的问题
export {}